from jili.trade.constants import openclose,status,bs,market
from jili.data.obj import calclongmargin,calcshortmargin,calclongpl,calcshortpl,getmarketcode
from jili.pos.module import base
class position(base):
    def __init__(self,
                 obj="",
                 bs="",
                 price=0,
                 qty=0,
                 todayqty=0,
                 yesterdayqty=0,
                 marketcode="",
                 avlqty=0,
                 avltodayqty=0,
                 avlyesterdayaty=0,
                 frozentodayqty=0,
                 frozenyesterdayqty=0,
                 frozenqty=0,
                 pl=0,
                 margin=0,
                 detaildict={}
                 ):
        super(position, self).__init__()
        self.obj = obj,
        self.bs = bs,
        self.price = price,
        self.qty = qty,
        self.todayqty = todayqty,
        self.yesterdayqty = yesterdayqty,
        self.avlqty = avlqty,
        self.avltodayqty = avltodayqty,
        self.avlyesterdayaty = avlyesterdayaty,
        self.frozenqty = frozenqty,
        self.frozentodayqty = frozentodayqty,
        self.frozenyesterdayqty = frozenyesterdayqty,
        self.marketcode = marketcode,
        self.pl = pl
        self.detaildict=detaildict
        self.frozendict={}
        self.margin=margin
    def calcpos(self,price):
        if self.bs==bs.buy:
            margin=calclongmargin(self.obj,price)*self.qty
        else:
            margin = calcshortmargin(self.obj, price)*self.qty
        return margin
    def calcpl(self,price):
        pl=0
        if self.bs==bs.buy:
            pl=calclongpl(self.obj,price,self.price)*self.qty
        else:
            pl = calcshortpl(self.obj, price, self.price) * self.qty
        return pl
    def update_orderdetail(self,oo):
        orderid = oo.orderid
        oc = oo.openclose
        stat = oo.status
        workingqty = oo.workingqty
        cancelqty = oo.cancelqty
        if oc == openclose.close:  # 处理fund
            if stat == status.working:  # 挂单冻结
                if orderid not in self.frozendict.keys():
                    self.frozendict[orderid] = workingqty
                    self.frozenqty=self.frozenqty+workingqty
                    self.avlqty=self.avlqty-workingqty
            elif stat == status.canceled:  # 撤单解冻
                if orderid in self.frozendict.keys():
                    if self.frozendict[orderid]!=0:
                        self.frozendict[orderid] = 0
                        self.frozenqty = self.frozenqty - cancelqty
                        self.avlqty=self.avlqty+cancelqty
        elif oc==openclose.closetoday:
            if stat == status.working:  # 挂单冻结
                if orderid not in self.frozendict.keys():
                    self.frozendict[orderid] = workingqty
                    self.frozenqty = self.frozenqty + workingqty
                    self.avlqty = self.avlqty - workingqty
                    self.frozentodayqty = self.frozentodayqty + workingqty
                    self.avltodayqty = self.avltodayqty - workingqty
            elif stat == status.canceled:  # 撤单解冻
                if orderid in self.frozendict.keys():
                    if self.frozendict[orderid] != 0:
                        self.frozendict[orderid] = 0
                        self.frozenqty = self.frozenqty - cancelqty
                        self.avlqty = self.avlqty + cancelqty
                        self.frozentodayqty = self.frozentodayqty - cancelqty
                        self.avltodayqty = self.avltodayqty + cancelqty
        elif oc ==openclose.closeyesterday:
            if stat == status.working:  # 挂单冻结
                if orderid not in self.frozendict.keys():
                    self.frozendict[orderid] = workingqty
                    self.frozenqty = self.frozenqty + workingqty
                    self.avlqty = self.avlqty - workingqty
                    self.frozenyesterdayqty = self.frozenyesterdayqty + workingqty
                    self.avlyesterdayaty = self.avlyesterdayaty - workingqty
            elif stat == status.canceled:  # 撤单解冻
                if orderid in self.frozendict.keys():
                    if self.frozendict[orderid] != 0:
                        self.frozendict[orderid] = 0
                        self.frozenqty = self.frozenqty - cancelqty
                        self.avlqty = self.avlqty + cancelqty
                        self.frozenyesterdayqty = self.frozenyesterdayqty - cancelqty
                        self.avlyesterdayaty = self.avlyesterdayaty + cancelqty
    def update_tradedetail(self, oo):
        obj = oo.obj
        bs = oo.bs
        orderid = oo.orderid
        oc = oo.openclose
        tradedqty = oo.tradedqty
        tradeid = oo.tradeid
        tprice=oo.tradedprice
        if oc==openclose.open:
            self.price = (tprice*tradedqty+self.price*self.qty)/(tradedqty+self.qty)
            self.qty=self.qty+tradedqty
            self.avlqty=self.avlqty+tradedqty
            self.detaildict[tradeid]=self.tradedetail2positiondetail(oo)
            if getmarketcode(obj)==market.SHFE:
                self.todayqty = self.todayqty + tradedqty
                self.avltodayqty = self.avltodayqty + tradedqty
        elif oc == openclose.close:  # 处理fund
            self.qty = self.qty - tradedqty
            self.frozenqty=self.frozenqty-tradedqty
        elif oc == openclose.closetoday:
            self.qty = self.qty - tradedqty
            self.todayqty = self.todayqty - tradedqty
            self.frozenqty = self.frozenqty - tradedqty
        elif oc == openclose.closeyesterday:
            self.qty = self.qty - tradedqty
            self.yesterdayqty = self.yesterdayqty - tradedqty
            self.frozenqty = self.frozenqty - tradedqty
    def update_positiondetail(self,posdetail):
        tradeid=posdetail.tradeid
        key=posdetail.tradedate+"_"+tradeid
        self.detaildict[key]=posdetail
    def tradedetail2positiondetail(self,oo):
        posdetail=positiondetail(
            obj=oo.obj,
            bs=oo.bs,
            price=oo.tradedprice,
            qty=oo.tradedqty,
            tradedate=oo.date,
            tradeid=oo.tradeid,
            tradetime=oo.updatetime,
        )
class positiondetail(base):
    def __init__(self,
                 obj="",
                 bs="",
                 price=0,
                 qty=0,
                 tradedate="",
                 tradeid="",
                 tradetime="",
                 ):
        super(positiondetail, self).__init__()
        self.obj = obj,
        self.bs = bs,
        self.price = price,
        self.qty = qty,
        self.tradedate = tradedate,
        self.tradeid = tradeid,
        self.tradetime = tradetime,
